/*
 * The MIT License
 * Copyright (c) 2012 kc4.org
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
package org.kc4.sso.auth;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;

/**
 * @author yoichi kikuchi
 *
 */
public class SSOClientInitParameters extends HashMap<String, String> {

	private static final long serialVersionUID = 1L;

	private static final String EMPTY = "";

	private static final String INIT_PARAM_NAME_USER_OBJECT = "UserObject";
	private static final String INIT_PARAM_NAME_STATIC_CALLBACK = "StaticCallback";

	private Class<?> userObject;
	private String staticCallback = EMPTY;

	private static SSOClientInitParameters instance;

	/**
	 * {@link SSOClientInitParameters} インスタンスを取得します。
	 *
	 * @return {@link SSOClientInitParameters} インスタンス。
	 */
	public static SSOClientInitParameters getInstance() {

		return getInstance(null);
	}

	/**
	 * {@link SSOClientInitParameters} インスタンスを取得します。
	 *
	 * @param params マッピングされたパラメーター。
	 * @return {@link SSOClientInitParameters} インスタンス。
	 */
	public static SSOClientInitParameters getInstance(Map<String, String> params) {

		if (instance == null) {

			try {

				instance = new SSOClientInitParameters(params);
			}
			catch (ServletException e) {

				throw new IllegalStateException(e);
			}
		}

		return instance;
	}

	/**
	 * インスタンスを生成し、{@link Map} からパラメーターを読み込みます。
	 *
	 * @param params マッピングされたパラメーター。
	 * @throws ServletException サーブレット例外。
	 */
	public SSOClientInitParameters(Map<String, String> params) throws ServletException {

		// 認証オブジェクトの型を取得
		String authorizedClassName = params.get(INIT_PARAM_NAME_USER_OBJECT);
		if (authorizedClassName != null) {

			try {

				this.setUserObject(Class.forName(authorizedClassName));
			}
			catch (ClassNotFoundException e) {

				throw new ServletException(e);
			}
		}

		String name;

		// 静的コールバック
		name = params.get(INIT_PARAM_NAME_STATIC_CALLBACK);
		if (name != null && !name.trim().isEmpty()) {

			this.setStaticCallback(name.trim());
		}
	}

	/**
	 * userObject を取得します。
	 *
	 * @return userObject オブジェクト。
	 */
	public Class<?> getUserObject() {
		return this.userObject;
	}

	/**
	 * userObject を設定します。
	 *
	 * @param userObject userObject オブジェクト。
	 */
	protected void setUserObject(Class<?> userObject) {
		this.userObject = userObject;
	}

	/**
	 * staticCallback を取得します。
	 *
	 * @return staticCallback オブジェクト。
	 */
	public String getStaticCallback() {
		return this.staticCallback;
	}

	/**
	 * staticCallback を設定します。
	 *
	 * @param staticCallback staticCallback オブジェクト。
	 */
	protected void setStaticCallback(String staticCallback) {
		this.staticCallback = staticCallback;
	}
}
